package com.example.redis;

import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@SpringBootApplication
@Controller
@RequestMapping("redis")
public class RedisApplication {

    public static void main(String[] args) {
        SpringApplication.run(RedisApplication.class, args);
    }


    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping("/get")
    @ResponseBody
    public String getRedis(@RequestParam(name = "key", defaultValue = "demo:key") String key,
                           @RequestParam(name = "value", defaultValue = "无关风月") String value) {
        redisTemplate.opsForValue().set(key, value);
        Object o = redisTemplate.opsForValue().get(key);
        return o == null ? "NULL" : o.toString();
    }

    @Autowired
    RedissonClient redisson;

    @PostMapping("getJson")
    @ResponseBody
    public JsonVo getJson(@RequestBody JsonVo vo, HttpServletRequest request) {
        
        return vo;
    }

    @PostMapping("getKey")
    @ResponseBody
    public String getKey(@RequestParam(name = "key", defaultValue = "demo:key") String key) {

        RMap<Object, Object> map = redisson.getMap("myMap");


        Object o = redisTemplate.opsForValue().get(key);
        return o == null ? "NULL" : o.toString();
    }

}
